import * as ReactDOM from "react-dom/client";
import { Provider } from "react-redux";
import { PersistGate } from "redux-persist/integration/react";
import zhCN from "antd/locale/zh_CN";
import { ConfigProvider } from "antd";
import { axios, injectStore } from "apis/module/axios";
import { RootState, persistor } from "apis/module/store";
import store from "apis/module/store";
import DevTools from "apis/module/store/DevTools";
import { disableReactDevTools } from "apis/module/store/DevTools";
import { setAppName, setAppVersion } from "apis/module/store/slices/appSlice";
import debug from "debug";
import App from "./App";
import { API_HOST } from "./config";
import "./main.css";

injectStore(store);

axios.defaults.baseURL = API_HOST;

if (process.env.NODE_ENV !== "production") {
  // 控制下日志输出, 默认是 app:*
  // app2 不输出日志
  localStorage.debug = "app:*";
}

const logger = debug("app:start");
logger("app start...");
logger("axios baseUrl is %o", axios.defaults.baseURL);

function renderApp() {
  console.log("[ 当前环境 REACT_APP_ENV : ]", process.env.REACT_APP_ENV);
  console.log("[ 当前环境 NODE_ENV : ]", process.env.NODE_ENV);
  const root = ReactDOM.createRoot(
    document.getElementById("application-root") as HTMLElement,
  );

  const rootState: RootState = store.getState();
  const theme = rootState.app.theme;
  store.dispatch(setAppName("one-ek"));
  store.dispatch(setAppVersion("1.0.1"));

  root.render(
    <Provider store={store}>
      <PersistGate loading={null} persistor={persistor}>
        <ConfigProvider locale={zhCN} theme={theme.config}>
          <App />
        </ConfigProvider>

        {process.env.NODE_ENV === "development" && <DevTools />}
      </PersistGate>
    </Provider>,
  );
}

if (process.env.NODE_ENV !== "development") {
  // 该方案有效
  console.log = () => {};
  console.error = () => {};
  console.debug = () => {};

  disableReactDevTools();
}

renderApp();
